package com.ylbh.modules.goodsorder.controller;

import java.util.List;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.goodsorder.dto.OrderConditionDTO;
import com.ylbh.common.pojo.goodsorder.vo.*;
import com.ylbh.common.pojo.goodsorder.dto.OrderShipmentDTO;
import com.ylbh.common.utils.CommonResult;
import com.ylbh.common.utils.Ref;
import com.ylbh.modules.goodsorder.service.ShoppingOrderFormService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ylbh.common.utils.PageUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * 商品订单
 * @author czx
 * @date 2019-04-26 14:47:48
 */
@RestController
@RequestMapping("shopping/order")
public class ShoppingOrderFormController {

    @Autowired
    private ShoppingOrderFormService shoppingOrderFormService;

    @ApiOperation("商城订单列表")
    @RequestMapping("/queryOrderByCondition")
    @RequiresPermissions("shopping:order:queryOrderByCondition")
    public CommonResult<PageUtils> queryOrderByCondition(OrderConditionDTO orderConditionDTO){
        PageUtils order = shoppingOrderFormService.queryOrderByCondition(orderConditionDTO);
        return Ref.ok(order);
    }

    @ApiOperation("商城订单详情")
    @RequestMapping("/getOrderById/{id}")
    @RequiresPermissions("shopping:order:getOrderById")
    public CommonResult<OrderDetailsVO> getOrderById(@PathVariable(value = "id")String id){
        OrderDetailsVO vo = shoppingOrderFormService.getOrderByIds(id);
        return Ref.ok(vo);
    }

    @ApiOperation("获取物流公司列表")
    @RequestMapping("/queryLogisticsCompany")
    @RequiresPermissions("shopping:order:queryLogisticsCompany")
    public CommonResult<List<LogisticsCompanyVO>> queryLogisticsCompany(){
        List<LogisticsCompanyVO> company = shoppingOrderFormService.queryLogisticsCompany();
        return Ref.ok(company);
    }

    @SysLog("订单发货")
    @ApiOperation("订单发货")
    @RequestMapping("/modifyOrderShipmentById/{id}/{shipId}/{shipCode}/{cartId}/{userId}/{orderId}")
    @RequiresPermissions("shopping:order:modifyOrderShipmentById")
    public CommonResult<Void> modifyOrderShipmentById(@PathVariable String id, @PathVariable String shipId,
                                                      @PathVariable String shipCode, @PathVariable String cartId,
                                                      @PathVariable String userId, @PathVariable String orderId){
        shoppingOrderFormService.modifyOrderShipmentById(id, shipId, shipCode, cartId, userId, orderId);
        return Ref.ok();
    }

    @SysLog("子订单发货")
    @ApiOperation("子订单发货")
    @RequestMapping("modifyOrderShipmentByCardId")
    @RequiresPermissions("shopping:order:modifyOrderShipmentById")
    public CommonResult<String> modifyOrderShipmentByCardId(@RequestBody OrderShipmentDTO orderShipmentDTO) {
        String msg = shoppingOrderFormService.modifyOrderShipmentByCardId(orderShipmentDTO);
        return Ref.ok(msg);
    }

    @ApiOperation("查看物流")
    @RequestMapping("/getLogisticsInformationByExpressNo/{shipCode}")
    @RequiresPermissions("shopping:order:getLogisticsInformationByExpressNo")
    public CommonResult<LogisticsInformationVO> getLogisticsInformationByExpressNo(@PathVariable(value = "shipCode")String shipCode) throws Exception{
        LogisticsInformationVO vo = shoppingOrderFormService.getLogisticsInformationByExpressNo(shipCode);
        return Ref.ok(vo);
    }

    @SysLog("修改物流前查出当前物流物流公司")
    @ApiOperation("修改物流前查出当前物流物流公司")
    @RequestMapping("/getExpressNameById/{id}")
    @RequiresPermissions("shopping:order:getExpressNameById")
    public CommonResult<ExpressCompanyVO> getExpressNameById(@PathVariable(value = "id")Long id) {
        ExpressCompanyVO vo = shoppingOrderFormService.getExpressNameById(id);
        return Ref.ok(vo);
    }

    @SysLog("修改物流")
    @ApiOperation("修改物流")
    @RequestMapping("/modifyExpressNameOrShipCodeById")
    public CommonResult<Void> modifyExpressNameOrShipCodeById(@RequestBody OrderShipmentDTO orderShipmentDTO) {
        shoppingOrderFormService.modifyExpressNameOrShipCodeById(orderShipmentDTO);
        return Ref.ok();
    }

    @SysLog("修改子订单物流信息")
    @ApiOperation("修改子订单物流信息")
    @RequestMapping("/modifyExpressNameOrDeliveryNoById")
    public CommonResult<Void> modifyExpressNameOrDeliveryNoById(@RequestBody OrderShipmentDTO orderShipmentDTO) {
        shoppingOrderFormService.modifyExpressNameOrDeliveryNoById(orderShipmentDTO);
        return Ref.ok();
    }

    @ApiOperation("导出商城订单")
    @RequestMapping("/orderExcel")
    @RequiresPermissions("shopping:order:orderExcel")
    public CommonResult<Void> orderExcel(HttpServletResponse response, HttpServletRequest request) throws NormalException {
        shoppingOrderFormService.orderExcel(response, request);
        return Ref.ok();
    }

    @ApiOperation("子订单物流公司")
    @RequestMapping("/getExpressNameByCartId/{id}")
    public CommonResult<ExpressCompanyVO> getExpressNameByCartId(@PathVariable(value = "id")Long id) {
        ExpressCompanyVO vo = shoppingOrderFormService.getExpressNameByCartId(id);
        return Ref.ok(vo);
    }

    @ApiOperation("发货时查出没有售后的订单信息")
    @RequestMapping("/notIssuedGoods/{id}")
    public CommonResult<List<SubOrderListVO>> notIssuedGoods(@PathVariable(value = "id")Long id) {
        List<SubOrderListVO> vo = shoppingOrderFormService.notIssuedGoods(id);
        return Ref.ok(vo);
    }

    @SysLog("通知供应商发货")
    @GetMapping("notifySupplier/{id}")
    public CommonResult notifySupplier(@PathVariable String id) {
        shoppingOrderFormService.notifySupplier(id);
        return Ref.ok();
    }
}
